<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'unfold', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('auth.rule.title') })"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" :pagination="false" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, provide } from 'vue'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { useI18n } from 'vue-i18n'
import { baTableApi } from '/@/api/common'
import baTableClass from '/@/utils/baTable'
import createAxios from '/@/utils/axios'

defineOptions({
    name: 'goods/cate',
})

const optButtons = defaultOptButtons(['edit', 'delete'])
const { t } = useI18n()
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/backend/goods-cate/'),
    {
        expandAll: true,
        dblClickNotEditColumn: [undefined, 'keepalive', 'status'],
        column: [
            { type: 'selection', align: 'center' },
            { label: t('goods.cate.title'), prop: 'title', align: 'left', showOverflowTooltip: true },
            { label: t('goods.cate.pic'), prop: 'pic', align: 'center', render: 'image', operator: false },
            //{ label: t('goods.cate.rank'), prop: 'rank', align: 'center', width: '80' },
            /*{
                label: t('goods.cate.is_propose'),
                prop: 'is_propose',
                align: 'center',
                render: 'tag',
                custom: { '0': 'danger', '1': 'success' },
                replaceValue: { '0': t('goods.cate.deny'), '1': t('goods.cate.yes') },
            },*/
            //{ label: t('State'), prop: 'status', align: 'center', width: '80', render: 'switch' },
            { label: t('Update time'), prop: 'mtime', align: 'center', width: '160', render: 'datetime' },
            {
                label: t('Operate'),
                align: 'center',
                width: '130',
                render: 'buttons',
                buttons: optButtons,
            },
        ],
        dragSortLimitField: 'pid',
    },
    {
        defaultItems: {
            type: 'menu',
            menu_type: 'tab',
            extend: 'none',
            keepalive: 0,
            status: '1',
            icon: 'fa fa-circle-o',
        },
    },
    {
        getLists: () => {
            baTable.table.expandAll = baTable.table.filter?.quickSearch ? true : false
        },
        // 切换表单后
        toggleForm({ operate }) {
            if (operate == 'Show') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Edit') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Add') {
                menuRuleTreeUpdate()
            }
        },
        // 编辑请求完成后
        requestEdit() {
            menuRuleTreeUpdate()
        },
        // 编辑请求完成后
        requestShow() {
            menuRuleTreeUpdate()
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getLists()?.then(() => {
        baTable.dragSort()
    })
})

const menuRuleTreeUpdate = () => {
    // 获取树状栏目
    createAxios({
        url: '/backend/goods-cate/tree',
        method: 'get',
    }).then((res) => {
        console.log('弹出修改前获取分类', res.data)
        console.log('弹出修改前', baTable.form.items)

        baTable.form.extend!.menuRules = res.data

        if (baTable.form.items!.rules && baTable.form.items!.rules.length) {
            if (baTable.form.items!.rules.includes('*')) {
                let arr: number[] = []
                for (const key in baTable.form.extend!.menuRules) {
                    arr.push(baTable.form.extend!.menuRules[key].id)
                }
                baTable.form.extend!.defaultCheckedKeys = arr
            } else {
                baTable.form.extend!.defaultCheckedKeys = baTable.form.items!.rules
            }
        } else {
            baTable.form.extend!.defaultCheckedKeys = []
        }
        baTable.form.extend!.treeKey = uuid()

        console.log('制作的弹出权限信息：选中的', baTable.form.extend!.defaultCheckedKeys)
    })
}
</script>

<style scoped lang="scss"></style>
